package com.icreate.storm.methods.mongo;

import com.mongodb.client.model.Filters;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.storm.mongodb.common.QueryFilterCreator;
import org.apache.storm.mongodb.common.SimpleQueryFilterCreator;
import org.apache.storm.tuple.ITuple;
import org.bson.conversions.Bson;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by feng on 2017/5/11.
 */
public class IndicatorQueryFilterCreator implements QueryFilterCreator {
    private Map<String, Boolean> fields;

    public Bson createFilter(ITuple tuple) {
        List<Bson> bsons = new LinkedList<>();
        for (Map.Entry<String, Boolean> entry : this.fields.entrySet()) {
            if (entry.getValue())
                bsons.add(Filters.eq(entry.getKey(), tuple.getValueByField(entry.getKey())));
            else
                bsons.add(Filters.eq(entry.getKey(), "-"));
        }
//        for ( field : this.fields.entrySet()) {
//            bsons.add(Filters.eq(field, tuple.getValueByField(field)));
//        }
        return Filters.and(bsons);
//        Bson eq= Filters.eq(this.field, tuple.getValueByField(this.field));
    }

    public IndicatorQueryFilterCreator withField(Map<String, Boolean> fields) {
        this.fields = fields;
        return this;
    }
}
